package com.wutao.panzh.admin.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panzh.entity.InformationItem;
import com.panzh.entity.SceneryItem;
import com.panzh.entity.index.IndexInformationItem;
import com.panzh.entity.index.IndexSceneryItem;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.admin.mapper.SceneryItemMapper;
import com.wutao.panzh.service.SceneryItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SceneryItemServiceImpl implements SceneryItemService {

    @Autowired
    private SceneryItemMapper sceneryItemMapper;

    @Override
    public PageInfo<SceneryItem> getSceneryItemPageInfo(Integer pageNo, Integer pageSize, String keyword) {
        //使用PageHelper启动分页；
        PageHelper.startPage(pageNo,pageSize);
        //一定要注意pageHelper的版本问题，写文档的时候进行查询
        //https://blog.csdn.net/qq_31204567/article/details/78172168；该网站会有你想要的信息
        List<SceneryItem> informationItems = sceneryItemMapper.selectFroSearch(keyword);
        //封装成PageInfo进行返回
        PageInfo<SceneryItem> pageInfo = new PageInfo<>(informationItems);
        return pageInfo;
    }

    @Override
    public SceneryItem getSceneryItemById(String sceneryId) {
        Integer id = Integer.parseInt(sceneryId);
        SceneryItem sceneryItem = sceneryItemMapper.selectSceneryItemById(sceneryId);
        return sceneryItem;
    }

    @Override
    public void deleteSceneryById(String sceneryId) {
        Integer id = Integer.parseInt(sceneryId);
        sceneryItemMapper.deleteSceneryImage(sceneryId);
        sceneryItemMapper.deleteSceneryById(id);
    }

    @Override
    public List<SceneryItem> getSceneryItemByItemName(String itemName) {
        List<SceneryItem> list = sceneryItemMapper.selectSceneryItemByItemName(itemName);
        return list;
    }

    @Override
    public void saveSceneryItem(SceneryItem sceneryItem) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        sceneryItem.setSceneryData(format);
        sceneryItemMapper.insertSceneryItem(sceneryItem);
    }

    @Override
    public void updateScenery(SceneryItem sceneryItem) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        sceneryItem.setSceneryData(format);
        sceneryItemMapper.updateSceneryItem(sceneryItem);
    }

    @Override
    public void insertSceneryImageSrc(String imagSrc, String sceneryItemId) {
        //先进行查询，如果查询有结果，就进行更新，如果查询没有结果，就进行插入；
        String imagSrcBySceneryItemId = sceneryItemMapper.selectImagSrcBySceneryItemId(sceneryItemId);
        if (imagSrcBySceneryItemId == null){
            //进行插入；
            sceneryItemMapper.insertImagSrcBySceneryItemId(imagSrc,sceneryItemId);
        }else {
            //进行更新；
            sceneryItemMapper.updateImagSrcBySceneryItemId(imagSrc,sceneryItemId);
        }
    }

    @Override
    public PageInfo<IndexSceneryItem> getAllIndexSceneryItem(Integer pageNo, Integer classificationId) {
        PageHelper.startPage(pageNo, ProjectParames.PAGE_SIZE);
        List<IndexSceneryItem> informationItems = sceneryItemMapper.selectIndexSceneryFroSearch(classificationId);
        //封装成PageInfo进行返回
        PageInfo<IndexSceneryItem> pageInfo = new PageInfo<>(informationItems);
        return pageInfo;
    }

}
